import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { useUserStore } from './user'
import { insertCartAPI, findNewCartListAPI, delCartAPI, updateCartAPI } from '@/apis/cart'

export const useCartStore = defineStore(
  'cart',
  () => {
    const userStore = useUserStore()
    const isLogin = computed(() => userStore.userInfo.token)
    const cartList = ref([])

    // 添加商品
    const addCart = async (good) => {
      const { skuId, count } = good

      if (isLogin.value) {
        // 登录后
        await insertCartAPI({ skuId, count })
        await updateNewList()
      } else {
        // 登录前
        const item = cartList.value.find((item) => item.skuId === good.skuId)
        if (item) {
          item.count += good.count
        } else {
          cartList.value.push(good)
        }
      }
    }

    // 删除商品
    const delCart = async (skuId) => {
      if (isLogin.value) {
        await delCartAPI([skuId])
        await updateNewList()
      } else {
        cartList.value = cartList.value.filter((item) => item.skuId !== skuId)
      }
    }

    // 获取购物车列表
    const updateNewList = async () => {
      const res = await findNewCartListAPI()
      cartList.value = res.result
    }

    // 清空购物车
    const clearCart = async () => {
      cartList.value = []
    }

    // 商品总数
    const allCount = computed(() => {
      return cartList.value.reduce((pre, cur) => {
        return pre + cur.count
      }, 0)
    })

    // 商品总价
    const allPrice = computed(() => {
      return cartList.value.reduce((pre, cur) => {
        return pre + cur.count * cur.price
      }, 0)
    })

    // 已选择数量
    const selectedCount = computed(() => {
      const selectedList = cartList.value.filter((item) => item.selected)
      return selectedList.reduce((pre, cur) => {
        return pre + cur.count
      }, 0)
    })

    // 已选择总价
    const selectedPrice = computed(() => {
      const selectedList = cartList.value.filter((item) => item.selected)
      return selectedList.reduce((pre, cur) => {
        return pre + cur.count * cur.price
      }, 0)
    })

    // 单选
    const singleCheck = async (skuId, selected) => {
      if (isLogin.value) {
        await updateCartAPI(skuId, selected)
        await updateNewList()
      } else {
        const item = cartList.value.find((item) => item.skuId === skuId)
        item.selected = selected
      }
    }

    // 全选状态
    const isAll = computed(() => {
      return cartList.value.every((item) => item.selected)
    })

    // 全选操作
    const allCheck = (selected) => {
      cartList.value.forEach((item) => {
        item.selected = selected
      })
    }

    return {
      cartList,
      addCart,
      delCart,
      allCount,
      allPrice,
      singleCheck,
      isAll,
      allCheck,
      selectedCount,
      selectedPrice,
      clearCart,
      updateNewList
    }
  },
  {
    persist: true
  }
)
